import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        //主页面
        {
            path: "/home",
            component: () => import('../views/home.vue'),
            redirect: '/home/userlist',
            children: [
                {
                    path: 'userlist',
                    component: () => import('../components/userlist.vue')
                },
                {
                    path: 'userinfo',
                    component: () => import('../components/userinfo.vue')
                },
                {
                    path: 'checkvuexpages',
                    component: () => import('../components/checkvuexpages.vue')
                },
            ]
        },
        //登录页面
        {
            path: "/login",
            component: () => import('../views/login.vue')
        },
        //直接报错
        // {
        //     path: "*",
        //     redirect: "/login"
        // },
        //正确写法：
        //1
        // {
        //     path: "/:catchAll(.*)",
        //     redirect: "/login"
        // },
        //2
        // {
        //     path: "/:pathMatch(.*)*",
        //     redirect: "/login"
        // },
        //3
        {
            path: "/:pathMatch(.*)",
            redirect: "/login"
        },


    ],
})
router.beforeEach((t, f, n) => {
    if (t.path.includes('login')) {
        n()
    }
    if (!localStorage.getItem('token')) {
        n('/login')
    } else {
        n()
    }
})
export default router